package ext;

/**
 * 两种方式实现二分搜索
 */
public class BinarySearch {
    //查找到第一个>=k的位置
    public static int firstNotLessThen(int[] nums, int target) {
        int start = 0, end = nums.length - 1;
        while (start < end) {
            int m = start + (end - start) / 2;
            if (nums[m] < target)
                start = m + 1;
            else
                end = m;
        }
        return start;
    }

    //查找到最后一个>=k的位置
    public static int lastNotLessThen(int[] nums, int target) {
        return firstNotLessThen(nums, target + 1) - 1;
    }

    public static void main(String[] args) {
        int target = 3;
        int[] nums0 = {2, 3};
        int[] nums1 = {1, 2, 3, 3, 4, 5};
        //System.out.println(firstNotLessThen(nums0, target));
        System.out.println(lastNotLessThen(nums0, target));
    }

}
